<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :xs="9" :sm="6" :md="5" :lg="4" :xl="4">
        <div class="head-container">
          <el-input
            v-model="disType"
            clearable
            size="small"
            placeholder="输入调度类型搜索"
            prefix-icon="el-icon-search"
            @input="getTypes"
          />
        </div>
        <el-tree
          :data="typeDatas"
          :props="defaultProps"
          @node-click="handleNodeClick"
        />
      </el-col>

      <el-col :xs="15" :sm="18" :md="19" :lg="20" :xl="20">
        <div class="head-container">
          <div v-if="crud.props.searchToggle">
            <!-- 搜索 -->
            <el-input v-model="query.name" clearable size="small" placeholder="请输入关键字" style="width: 300px; margin-bottom: 20px" class="content" @keyup.enter.native="crud.toQuery" />
            <rrOperation style="margin: 20px" />
          </div>
          <crudOperation :permission="permission" />
        </div>

        <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="520px">
          <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="150px">
            <el-form-item label="调度单位名称" prop="name">
              <el-input v-model="form.name" style="width: 280px;" />
            </el-form-item>
            <el-form-item label="关联组织机构" prop="oid">
              <el-select v-model="form.oid" placeholder="请选择" style="width: 280px;">
                <el-option label="西北" value="西北" />
                <el-option label="陕西" value="陕西" />
              </el-select>
            </el-form-item>
            <el-form-item label="所属上级调度单位" prop="pid">
              <el-select v-model="form.pid" placeholder="请选择" style="width: 280px;">
                <el-option label="西北网调" value="西北网调" />
                <el-option label="陕西省调" value="陕西省调" />
              </el-select>
            </el-form-item>
            <el-form-item label="调度性质" prop="type">
              <el-select v-model="form.type" placeholder="国调、网调、省调、地调" style="width: 280px;">
                <el-option label="国调" value="国调" />
                <el-option label="网调" value="网调" />
                <el-option label="省调" value="省调" />
                <el-option label="地调" value="地调" />
                <!--                <el-option-->
                <!--                  v-for="item in typeList"-->
                <!--                  :key="item.dictSort"-->
                <!--                  :label="item.label"-->
                <!--                  :value="item.dictSort"-->
                <!--                />-->
              </el-select>
            </el-form-item>
            <el-form-item label="简称" prop="shortName">
              <el-select
                v-model="form.shortName"
                style="width: 150px"
                placeholder="陕、甘、青"
              >
                <el-option
                  v-for="item in shortNameList"
                  :key="item.dictSort"
                  :label="item.label"
                  :value="item.dictSort"
                />
              </el-select>
            </el-form-item>

          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="text" @click="crud.cancelCU">取消</el-button>
            <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
          </div>
        </el-dialog>

        <el-table ref="table" v-loading="crud.loading" highlight-current-row style="width: 100%;" :data="crud.data" @selection-change="crud.selectionChangeHandler">
          <el-table-column type="selection" width="50" />
          <el-table-column prop="name" label="调度单位名称" />
          <el-table-column prop="oid" label="关联组织机构名称" />
          <el-table-column prop="pid" label="所属上级调度单位" />
          <el-table-column prop="type" label="调度性质" />
          <el-table-column prop="shortName" label="简称" />
          <el-table-column width="135px" prop="createTime" label="创建日期" />
          <el-table-column v-if="checkPer(['admin','sysDispatchUnit:edit','sysDispatchUnit:del'])" label="操作" width="130px" align="center" fixed="right">
            <template slot-scope="scope">
              <udOperation
                :data="scope.row"
                :permission="permission"
              />
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <pagination />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import crudDisUnit from '@/api/system/disunit'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'

const defaultForm = { id: null, name: null, oid: null, pid: null, shortName: null, createTime: null }

export default {
  name: 'Disunit',
  components: { pagination, rrOperation, crudOperation, udOperation },
  cruds() {
    return CRUD({ title: '调度单位', url: 'api/sysDispatchUnit', sort: 'level,asc', crudMethod: { ...crudDisUnit }})
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  data() {
    return {
      disType: '',
      shortNameList: [],
      typeDatas: [],
      types: [],
      typeList: [],
      defaultProps: { label: 'label', value: 'value' },
      currentId: 0,
      permission: {
        add: ['admin', 'sysDispatchUnit:add'],
        edit: ['admin', 'sysDispatchUnit:edit'],
        del: ['admin', 'sysDispatchUnit:del']
      },
      rules: {
        name: [
          { required: true, message: '请输入调度单位名称', trigger: 'blur' }
        ],
        oid: [
          { required: true, message: '请选择关联组织机构', trigger: 'blur' }
        ],
        pid: [
          { required: true, message: '请选择所属上级调度单位', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择调度性质', trigger: 'blur' }
        ]
      }
    }
  },
  // 初始加载
  created() {
    this.selectShortName()
    this.selectType()
    this.getTypes()
  },
  methods: {
    // 改变数据
    update() {
      // 无刷新更新 表格数据
      crudDisUnit.get(this.currentId).then(res => {
        for (let i = 0; i < this.crud.data.length; i++) {
          if (res.id === this.crud.data[i].id) {
            this.crud.data[i] = res
            break
          }
        }
      })
    },
    // 下拉显示简称
    selectShortName() {
      crudDisUnit.selectShortName().then(res => {
        this.shortNameList = res
      }).catch(() => { })
    },
    // 下拉显示调度类型
    selectType() {
      const params = { name: null }
      crudDisUnit.getTypeNames(params).then(res => {
         this.typeList = res.content
      }).catch(() => {})
    },
    // 获取左侧数据
    getTypes() {
      const params = { name: this.disType }
      crudDisUnit.getTypeNames(params).then(res => {
        this.typeDatas = res.content
      }).catch(() => { })
    },
    // 切换
    handleNodeClick(data) {
      if (data.id === null) {
        this.query.type = null
      } else {
        this.query.type = data.label
      }
      this.crud.toQuery()
    }
  }
}
</script>

<style scoped>
  .content{
    vertical-align:middle;
  }
</style>
